package com.ansion.basedb.storage;

import android.annotation.SuppressLint;
import android.text.TextUtils;

import com.ansion.basetool.R;
import com.ansion.string.StringUtils;
import com.ansion.user.beanforui.ContactInfo;
import com.ansion.utils.ContactUtil;
import com.ansion.utils.ResourceUtils;

import java.util.List;
import java.util.Locale;

/***
 * @author john 2024-03-01
 * 用来生成DisplayName，与UserModel和ContactsModel解耦合
 */
public class DisplayNameUtil {


    public static String getDisplayName(String uid, String nickName, boolean isContact, String firstName, String middleName, String lastName, boolean formatPhone) {
        if (ContactUtil.isSelf(uid)) {
            return nickName;
        }
        if (!isContact) {//非系统联系人
            return formatPhone ? ContactUtil.getFormatPhone(uid) : "+" + uid;
        }
        //是系统联系人
        String contactDisplayName = getContactDisplayName(firstName, middleName, lastName);
        if (!TextUtils.isEmpty(contactDisplayName)) {
            return contactDisplayName;
        } else {
            String result = formatPhone ? ContactUtil.getFormatPhone(uid) : "+" + uid;
            return ResourceUtils.getString(R.string.bot_uid_ltr, result);
        }
    }

    public static String getContactDisplayName(String firstName, String middleName, String lastName) {
        if (StringUtils.isContainsChinese(firstName)
                || StringUtils.isContainsChinese(lastName)
                || StringUtils.isContainsChinese(middleName)) {
            return getChineseContactName(firstName, middleName, lastName);
        } else if (!TextUtils.isEmpty(firstName)
                && !TextUtils.isEmpty(lastName)) {
            return firstName + " " + lastName;
        } else if (!TextUtils.isEmpty(firstName)) {
            return firstName;
        } else if (!TextUtils.isEmpty(lastName)) {
            return lastName;
        }
        return "";
    }

    private static String getChineseContactName(String firstName, String middleName, String lastName) {
        String name = "";
        if (lastName != null) {
            name += lastName;
        }
        if (middleName != null) {
            name += middleName;
        }
        if (firstName != null) {
            name += firstName;
        }
        return name;
    }



    @SuppressLint("DefaultLocale")
    public static String buildDisplayName(List<ContactInfo> contactInfos, boolean p2pCall, int memberCount) {
        if (p2pCall)
            return contactInfos.get(0).getDisplayName();
        else {
            if (contactInfos.isEmpty()) {
                return "";
            }
            if (contactInfos.size() == 1) {
                ContactInfo contactInfo = contactInfos.get(0);
                if (memberCount <= 2) {
                    return String.format("%s", contactInfo.getDisplayName());
                }
                return String.format("%s & %d others", contactInfo.getDisplayName(), memberCount - 2);
            } else if (contactInfos.size() <= 3) {
                StringBuilder sb = new StringBuilder();
                for (ContactInfo contactInfo : contactInfos) {
                    sb.append(contactInfo.getDisplayName()).append(",").append(" ");
                }
                return sb.substring(0, sb.length() - 2);
            } else {
                return String.format(Locale.ENGLISH, "%s , %s & %d others", contactInfos.get(0).getDisplayName(), contactInfos.get(1).getDisplayName(), memberCount - 2);
            }
        }
    }
}
